package com.doctor.windflower_doctor.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.entity.MessageBeen;
import com.doctor.windflower_doctor.view.RoundedImageView;
import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

public class MessageOneAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater inflater;
	private List<MessageBeen> list;
	private DisplayImageOptions options;

	public MessageOneAdapter(Context context) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.list = new ArrayList<MessageBeen>();
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.loading_img).showImageOnFail(R.drawable.list_default_icon)
				.showImageForEmptyUri(R.drawable.list_default_icon).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.ARGB_8888).build();
	}

	public void setData(List<MessageBeen> list) {
		this.list = list;
	}

	public void addData(List<MessageBeen> data) {
		if (data != null) {
			this.list.addAll(data);
		}
		notifyDataSetChanged();
	}

	public void clear() {
		if (list != null) {
			list.clear();
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.one_item, arg2, false);
			holder.img = (RoundedImageView) convertView.findViewById(R.id.imageView1);
			holder.tv_content = (TextView) convertView.findViewById(R.id.tv_commit);
			holder.tv_nick = (TextView) convertView.findViewById(R.id.tv_user);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		SystemApplication.getInstance().mImageLoad1.displayImage( list.get(arg0).imgUrl,holder.img,options);
		if (list.get(arg0).imgUrl == null) {
			holder.img.setImageResource(R.drawable.more);
		}
		holder.tv_content.setText(list.get(arg0).content);
		holder.tv_nick.setText(list.get(arg0).nick);
		holder.tv_time.setText(list.get(arg0).createTime);
		return convertView;
	}

}

class ViewHolder {
	RoundedImageView img;
	TextView tv_nick;
	TextView tv_time;
	TextView tv_content;

}
